package com.wgts.wgts.mvp.presenter.imp;

import com.wgts.wgts.mvp.model.IWgtsNomalMarketActivityModel;
import com.wgts.wgts.mvp.model.IWgtsProductModel;
import com.wgts.wgts.mvp.presenter.IWgtsNomalMarketActivityPresenter;
import com.wgts.wgts.mvp.view.IWgtsNomalMarketActivityView;
import com.wgts.wgts.netmodle.HttpResultSubscriber;
import com.wgts.wgts.netmodle.req.AppModleRequest;
import com.wgts.wgts.netmodle.req.WgtsProductListReq;
import com.wgts.wgts.netmodle.resp.AppModleEntity;
import com.wgts.wgts.netmodle.resp.WgtsProductListEntity;

import java.util.List;

public class WgtsNomalMarketActivityPresenter implements IWgtsNomalMarketActivityPresenter {

    private IWgtsNomalMarketActivityModel marketActivityModel;

    private IWgtsNomalMarketActivityView view;

    private IWgtsProductModel model;

    private String productType;

    private int pageIndex = 1;

    private int pageSize = 20;

    public WgtsNomalMarketActivityPresenter(IWgtsNomalMarketActivityModel marketActivityModel, IWgtsNomalMarketActivityView view, IWgtsProductModel model) {
        this.marketActivityModel = marketActivityModel;
        this.view = view;
        this.model = model;
    }

    @Override
    public void initBanner() {
    }

    @Override
    public void initProduct(String productType) {
        this.productType = productType;
        this.pageIndex = 1;
        WgtsProductListReq req = new WgtsProductListReq();
        req.pageIndex = String.valueOf(pageIndex);
        req.pageSize = String.valueOf(pageSize);
        req.productType = this.productType;
        model.initProducteList(req, new HttpResultSubscriber<List<WgtsProductListEntity>>() {
            @Override
            public void onSuccess(List<WgtsProductListEntity> wgtsProductListReqs) {
                if(wgtsProductListReqs.size() < pageSize){
                    view.initProductList(wgtsProductListReqs, true);
                }else{
                    view.initProductList(wgtsProductListReqs, false);
                    pageIndex++;
                }
            }

            @Override
            public void _onError(int status, String msg) {
                view.onError(status, msg);
            }
        });
    }

    @Override
    public void moreProduct() {
        WgtsProductListReq req = new WgtsProductListReq();
        req.pageIndex = String.valueOf(pageIndex);
        req.pageSize = String.valueOf(pageSize);
        req.productType = this.productType;
        model.initProducteList(req, new HttpResultSubscriber<List<WgtsProductListEntity>>() {
            @Override
            public void onSuccess(List<WgtsProductListEntity> wgtsProductListReqs) {
                if(wgtsProductListReqs.size() < pageSize){
                    view.moreProductList(wgtsProductListReqs, true);
                }else{
                    view.moreProductList(wgtsProductListReqs, false);
                    pageIndex++;
                }
            }

            @Override
            public void _onError(int status, String msg) {
                view.onError(status, msg);
            }
        });
    }


    @Override
    public void initDataView() {
        AppModleRequest appModleRequest = new AppModleRequest();
        appModleRequest.scene = "1";
        appModleRequest.parentTypeId = -1;
        model.initDataView(appModleRequest, new HttpResultSubscriber<List<AppModleEntity>>() {
            @Override
            public void onSuccess(List<AppModleEntity> appModleEntities) {
                view.initResult(appModleEntities);
            }

            @Override
            public void _onError(int status, String msg) {
                view.onError(status, msg);
            }
        });
    }
}
